<?php
namespace app\api\model;

use think\Model;
use think\Db;
use \tp5er\Backup;
use \think\Config;
//
// +--------------------------------------+
// |PHP version 5.5
// +--------------------------------------+
// |后台管理权限模型
// +--------------------------------------+
// |2018/01/09
// +--------------------------------------+
// |罗彪
// +--------------------------------------+
// |知果汇
// +--------------------------------------+
class Mysql extends Common{
    public function dataList(){
        $config = \think\Config::get('tp_mysql');
        $db= new Backup($config);
        $dataList = $db->dataList();
        $data = [];
        $count = 0;
        if(is_array($dataList)){
            foreach($dataList as $key=>$val){
                $data['info'][$key]['name'] = $val['name'];
                $data['info'][$key]['data_length'] = $val['data_length'];
                $count += $val['data_length'];
            }
            $data['count'] = $count;
            return $data;
        }else{
            return "";
        }
    }

    public function dataAdd(){
        $data['name'] = input("post.url");
        $data['size'] = input("post.count");
        $data['time'] = time();
        //$add = Db::connect("db_mongo")->name("mysql")->insert($data);
        $add = Db::name("mysql") -> insert($data);
        if($add){
            return "success";
        }else{
            $this -> deldir($url);
            return "error";
        }
    }
    
    /*
      数据库备份
     */
    public function start(){
        $url = input("post.url");
        $name = input("post.name");
        $_count = input("post._count");
        $url = './Mysql/'.$url.'/';
        $config = Config::get('tp_mysql');
        $config['path'] = $url;
        $db= new Backup($config);
        $file = ['name'=>$name, 'part' => $_count];
        $start= $db->setFile($file)->backup($name,0);
        if($start == 0){
            return "success";
        }else{
            $this -> deldir($url);
            return "error";
        }
    }

    public function backup_list(){
        $filename = input("post.filename");
        $url = './Mysql/'.$filename.'/';
        $config = Config::get('tp_mysql');
        $config['path'] = $url;
        $db= new Backup($config);
        $result = $db->fileList();
        if(!empty($result)){
            return $this->msg("success",['info'=>$result]);
        }else{
            return $this->msg("error");
        }
    }
    
    public function rev(){
        $file_name = input("post.file_name");
        $filename = input("post.filename");
        $url = './Mysql/'.$filename.'/'.$file_name;
        $info= $this->_import($url,$file_name);
        return $info;
    }

    public function _import($url,$filename){
        $reg = "/^.*-(.*)\.sql$/";
        preg_match($reg,$filename,$size);
        $size = (int)$size[1];
        $gz = fopen($url, 'r');
        $sql = '';
        while(!feof($gz)){
            $sql .= trim(fgets($gz));
        }
        $sql_arr = explode(';',$sql); 
        foreach($sql_arr as $val){
            if(!empty($val)){
                try{
                    Db::execute($val);
                }catch(Exception $e){
                    return $this->msg("error");
                }
                
            }
        }
        return $this->msg("success",['size'=>$size]);
    }
}
?>